package com.example.demo111.service.impl;

import com.example.demo111.entity.Project;
import com.example.demo111.entity.UserInfo;
import com.example.demo111.mapper.ProjectMapper;
import com.example.demo111.mapper.UserInfoMapper;
import com.example.demo111.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.demo111.entity.Record;




@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private ProjectMapper projectMapper;


    public Integer selectByNameAndPwd(UserInfo userInfo) {
        int code = 0;
        try {
            UserInfo a =this.userInfoMapper.selectByNameAndPwd(userInfo.getName(),userInfo.getPwd());
            if(a!=null){
                this.userInfoMapper.userInsertNO(a.getUid());
                UserInfo as=this.userInfoMapper.search();
                if(a.getRole()==1){

                    code= 2;
                }else {
                    code= 1;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return code;
        }
        return code;
    }

    @Override
    public Integer Userzc(UserInfo userInfo) {
        int code = 1002;
        //数据库设置唯一约束 如果用户名存在 则会抛出异常
        try {
            if(userInfo.getPwd()==null)return code;
            userInfo.setRole(2);
            int res = this.userInfoMapper.userInsert(userInfo);
            if (res > 0) {
                code = 1001;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return code;
        }
        return code;
    }


    public void updatePassword(String name,String pwd,String npwd){
        UserInfo userInfo1 = userInfoMapper.selectUser(name,pwd);
        if (userInfo1==null)throw new RuntimeException("请重试");
        this.userInfoMapper.updatePassword(name,pwd,npwd);
    }

    @Override
    public UserInfo search() {
        UserInfo a=this.userInfoMapper.search();
        return a;
    }


    @Override
    public void userVote(Integer pid, Integer uid) {
        this.userInfoMapper.userVote(pid, uid);
    }

    @Override
    public Record selectVoteInfo(Integer pid, Integer uid) {
        return this.userInfoMapper.selectVoteInfo(pid,uid);
    }

    @Override
    public void VoteAdd(Integer id) {
        Project p = this.projectMapper.getProId(id);
        this.userInfoMapper.VoteAdd(p.getNum()+1,p.getPid());

    }

}

